#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 14/ago/2011

@author: antstud
'''

from koro.core.context import Context
from koro.modules.prodotti.model import Prodotto,CategoriaProdotto
from koro.modules.commerciale.model import Listino
from koro.db import database

#===============================================================================
# ProdottiContext
#===============================================================================
class ProdottiContext(Context):
    def __init__(self):
        Context.__init__(self,Prodotto)
    
    def new_object(self):                
        new_object=Prodotto()
        database.get_instance().save(new_object)
        listini=database.get_instance().query(Listino).\
        order_by(Listino.ID).all()
        new_object.init_prezzi(listini)
        database.get_instance().commit()        
        return new_object                    
    
    def delete_object(self,obj):
        db_instance=database.get_instance()
        db_instance.add(obj)
        db_instance.flush()
        db_instance.remove(obj)        
        db_instance.commit()
    
    def create_object(self,descrizione,fornitore,cod_prod_forn,prezzo,iva):
        new_prodotto=self.new_object()
        if not descrizione.lstrip():
            descrizione='Prodotto {0}'.format(new_prodotto.ID)
        new_prodotto.descrizione=descrizione
        if fornitore is not None:
            new_prodotto.add_info_fornitore(fornitore, cod_prod_forn, prezzo,True)
        else:
            new_prodotto.set_prezzo(self._get_options().LISTINO_DEFAULT_VENDITA, prezzo)
        new_prodotto.set_aliquota_iva(iva)
        database.get_instance().commit()        
        return new_prodotto
    
    def lookup_by_codforn(self,pattern):
        query=database.get_instance().query(self._context)
        statement="SELECT * FROM prodotti, info_fornitore \
        where prodotti.ID = info_fornitore.fk_prodotto and \
        info_fornitore.codice_prod_forn LIKE '%{0}%'".format(pattern)
        query=query.from_statement(statement)
        return query.all()


#===============================================================================
# CategorieProdottiContext
#===============================================================================
class CategorieProdottiContext(Context):
    def __init__(self):
        Context.__init__(self,CategoriaProdotto)

    def get_objects(self,*args,**kwargs):
        query=database.get_instance().query(self._context).filter_by(categoria_padre=None)
        if len(kwargs)>0:
            query=query.filter_by(**kwargs)
        if len(args)>0:
            for a in args:
                query=query.filter(a)
        return query.all()
    
    def get_sottocategorie(self,padre):
        query=database.get_instance().query(self._context).filter_by(categoria_padre=padre.ID)
        return query.all()
    
    def add_sottocategoria(self,obj):
        database.get_instance().save(obj)
    
    def remove_sottocategoria(self,obj):
        database.get_instance().remove(obj)

    def delete_object(self,obj):
        db_instance=database.get_instance()
        for item in obj.sottocategorie:
            db_instance.remove(item)
        db_instance.delete(obj)
        
    def autodelete_object(self,obj):        
        Context.autodelete_object(self, obj)
    
    def expunge_object(self,obj):        
        db_instance=database.get_instance()
        for item in obj.sottocategorie:
            db_instance.expunge(item)
        db_instance.expunge(obj)

    def new_object(self):
        new_object=CategoriaProdotto()
        database.get_instance().save(new_object)
        return new_object 

    
    